package cn.cew.bytespyserver.tool;

import org.springframework.boot.system.ApplicationHome;

import java.io.File;
import java.util.*;

/**
 * @author chenenwei
 * @description TODO
 */
public class BaseUtils {

    public static boolean isEmpty (Object obj) {
        if (null == obj) {
            return true;
        }
        if (obj instanceof String) {
            return "".equals(obj);
        } else if (obj instanceof List) {
            return 0 == ((List)obj).size();
        } else if (obj instanceof Map) {
            return 0 == ((Map)obj).size();
        } else if (obj instanceof Set) {
            return 0 == ((Set)obj).size();
        } else if (obj instanceof Object[]) {
            return 0 == ((Object[])obj).length;
        } else if (obj instanceof int[]) {
            return 0 == ((int[])obj).length;
        } else if (obj instanceof long[]) {
            return 0 == ((long[])obj).length;
        } else if (obj instanceof String[]) {
            return 0 == ((String[])obj).length;
        }
        return true;
    }

    public static boolean isNotEmpty (Object obj) {
        return !isEmpty(obj);
    }

    /**
     * 获取jar路径的上一级路径
     * @return
     */
    public static String getProjectPath () {
        ApplicationHome applicationHome = new ApplicationHome(BaseUtils.class);
        File jarFile = applicationHome.getSource();
//        return jarFile.getAbsoluteFile().toString();
        return "D:\\bytespy";
    }

    public static String getProjectPath (String path) {
        ApplicationHome applicationHome = new ApplicationHome(BaseUtils.class);
        File jarFile = applicationHome.getSource();
//        return jarFile.getAbsoluteFile().toString();
        return "D:\\bytespy" + path;
    }
    public static String append (String str, String left, String right) {

        return  left + str + right;
    }

    public static String append (String str) {

        return  "(" + str + ")";
    }

    public static List<String> toList (String str, String indent) {
        List<String> list = new ArrayList<>();
        if (isNotEmpty(str)) {
            list.addAll(Arrays.asList(str.split(indent)));
        }
        return list;
    }

    public static String packageToDot(String className) {

        return className.replaceAll("/", ".");
    }
}
